package com.itheima.controller;

import com.itheima.pojo.Book;
import com.itheima.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 刘全
 * @date 2021/9/15
 */
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping
    public List<Book> getAll() {

        List<Book> books = bookService.getAll();

        /*Book b1 = new Book();
        Book b2 = new Book();

        List<Book> list = new ArrayList<>();

        b1.setId(1);
        b1.setType("成人读物");
        b1.setName("金瓶梅");
        b1.setDescription("金色瓶子装着的梅花");

        b2.setId(2);
        b2.setType("儿童读物");
        b2.setName("小蝌蚪找妈妈");
        b2.setDescription("很正经的读物,大概");

        list.add(b1);
        list.add(b2);*/

        return books;
    }

    @PostMapping
    public String save(@RequestBody Book book) {

        bookService.save(book);
        return "ok";
    }

    @DeleteMapping("/{id}")
    public String move(@PathVariable int id) {

        bookService.move(id);
        return "ok";
    }

    @PutMapping
    public String set(@RequestBody Book book) {
        //int i = 1/0;
        bookService.set(book);
        return "ok";
    }

    @GetMapping("/{name}")
    public List<Book> search(@PathVariable String name) {
        List<Book> books = bookService.search(name);
        System.out.println(books);
        return books;
    }
}
